<?php
//image module

class event_module {
	
	#display events
		
	public function upcoming()
	{
		
		$query = new Query("SELECT * FROM `events` WHERE `date` >= '" . time() . "' ORDER BY `date` ");
		if($query->num_rows() > 0)
		{
			$events = event::RecordSet($query, 'event');
			
			return self::display($events);
		}
		else
		{
			return '<p>There are currently no upcoming events.</p>';
		}
	}
					
	public function recent()
	{
		$query = new Query("SELECT * FROM `events` WHERE `date` < '" . time() . "' AND `date` >= '" . mktime(0, 0, 0, date('n', time()), 1, date('Y', time())) . "' ORDER BY `date` DESC");
		
		if($query->num_rows() == 0 )
		{
			$query = new Query("SELECT * FROM `events` WHERE `date` <'" . time() . "' ORDER BY `id` DESC LIMIT 0, 1");
		}
			
		if($query->num_rows() > 0)
		{
			$events = event::RecordSet($query, 'event');
			
			return self::display($events);
			
		}
		else
		{
			return '<p>There are currently no recent events.</p>';
		}
	}
			
	public function display($events)
	{
		$show .= '<table class="events">
							<tr>
								<th>Event</th>
								<th>Location</th>
								<th>Date</th>
							</tr>';
							
		foreach($events as $event)
		{
			$show .= '<tr>
						<td><a href="' . ROOT_URL . URL_BASE . 'event/' . $event->id . '">' .$event->title .'</a></td>
						<td> ' .$event->location .'</td>
						<td> ' .format::date($event->date) .'</td>
					 </tr>';
		}
		
		$show .= '</table>';
		
		return $show;
		
	}
	
	public function displayYear($year)
	{
		$start = mktime(0, 0, 0, 1, 1, $year);
		$end = mktime(0, 0, 0, 12, 31, $year);
		
		
		$query = new Query("SELECT * FROM `events` WHERE `date` >= " . $start . " AND `date` <= " . $end . " ORDER BY `date` ASC");
		
		if($query->num_rows() > 0)
		{
			$events = event::RecordSet($query, 'event');
			
			return self::display($events);
		}
		else
		{
			return 'No events for this year';
		}
		
	}
	
	public function archiveYears()
	{
		$nextYear = true;
		$year = date('Y', time()) - 1;
		
		while($nextYear)
		{
			$start = mktime(0, 0, 0, 1, 1, $year);
			$end = mktime(0, 0, 0, 12, 31, $year);
			
			
			$query = new Query("SELECT * FROM `events` WHERE `date` >= " . $start . " AND `date` <= " . $end . " ORDER BY `date` ASC");
			if($query->num_rows() > 0 AND $year)
			{
				$show .= '<p><a href="' . ROOT_URL . URL_BASE . 'event/archive/' . $year .'">' . $year . '</a></p>';
			}
			
			$query = new Query("SELECT * FROM `events` WHERE `date` <= " . $end . " ORDER BY `date` ASC");
			
			if($query->num_rows() <= 0 )
			{
				$nextYear = false;
			}
		
			$year--;
		}
		
		return $show;
	}
}
?>